//
//  CCMeViewController.m
//  BuDeJie
//
//  Created by demoncraz on 2017/2/9.
//  Copyright © 2017年 demoncraz. All rights reserved.
//

#import "CCMeViewController.h"
#import "CCSettingsViewController.h"
#import <AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import "CCSquareItem.h"
#import "CCCollectionViewCell.h"
#import "CCWebViewController.h"


static NSString *const cellId = @"cell";

static NSInteger cols = 4;
static CGFloat margin = 1;
#define itemWH (CCScreenW - margin * (cols - 1)) / cols


@interface CCMeViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) NSMutableArray *squareItems;

@property (nonatomic, weak) UICollectionView *collectionView;

@end

@implementation CCMeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor lightGrayColor];
    
    [self setupNavBar];
    
    //设置底部宫格视图
    [self setupFooterView];
    
    //请求网络数据
    [self loadData];
    
    //处理cell间距
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 15;
    self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
    
}

- (void)loadData {
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    parameters[@"a"] = @"square";
    parameters[@"c"] = @"topic";
    
    [mgr GET:@"http://api.budejie.com/api/api_open.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * responseObject) {
        
        //icon name url
        NSMutableArray *itemsArr = responseObject[@"square_list"];
        //创建模型数组
        self.squareItems = [CCSquareItem mj_objectArrayWithKeyValuesArray:itemsArr];
        
        //添加空白square补全
        [self resolveData];
        
        [self.collectionView reloadData];
        
        //重新计算collectionView的高度
        NSInteger count = self.squareItems.count;
        NSInteger rows = (count - 1) / cols + 1;
        CGFloat newHeight = (itemWH + margin) * rows;
        self.collectionView.CC_height = newHeight;
        
        //重新设置footerView重置可滚动范围
        self.tableView.tableFooterView = self.collectionView;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

- (void)resolveData {
    NSInteger count = self.squareItems.count;
    NSInteger extra = cols - count % cols;
    for (int i = 0; i < extra; i++) {
        CCSquareItem *item = [[CCSquareItem alloc] init];
        [self.squareItems addObject:item];
    }
}

- (void)setupFooterView {
    
    //创建一个collectionView
    //1.创建一个流水布局
    //2.创建collectionView
    //3.自定义collectionViewCell
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    
    layout.itemSize = CGSizeMake(itemWH, itemWH);
    layout.minimumLineSpacing = margin;
    layout.minimumInteritemSpacing = margin;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 300) collectionViewLayout:layout];
    collectionView.scrollEnabled = NO;
    _collectionView = collectionView;
    
    collectionView.backgroundColor = [UIColor clearColor];
    
    
    self.tableView.tableFooterView = collectionView;
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    [collectionView registerNib:[UINib nibWithNibName:@"CCCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cellId];
//    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellId];
    
    
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CCSquareItem *item = self.squareItems[indexPath.row];
    //点击打开网页
    if (![item.url containsString:@"http"]) {
        return;
    }
    NSURL *url = [NSURL URLWithString:item.url];
    
    CCWebViewController *webVc = [[CCWebViewController alloc] init];
    webVc.url = url;
    [self.navigationController pushViewController:webVc animated:YES];
    
}

#pragma mark - UICollectionViewDataSource


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.squareItems.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CCCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    
    cell.item = self.squareItems[indexPath.row];
    
    return cell;
}


- (void)setupNavBar {
    
    //添加右上角barButtonItem
    UIBarButtonItem *nightItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageOriginalWithName:@"mine-moon-icon_20x20_"] selImage:[UIImage imageOriginalWithName:@"mine-sun-icon_20x20_"] target:self action:@selector(nightClick:)];
    
    UIBarButtonItem *settingItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageOriginalWithName:@"mine-setting-iconN_20x20_"] highImage:[UIImage imageOriginalWithName:@"mine-setting-icon-clickN_20x20_"] target:self action:@selector(settingClick)];
    
    self.navigationItem.rightBarButtonItems = @[settingItem, nightItem];
    //添加titleView
    self.navigationItem.title = @"我的";
    
}

- (void)nightClick:(UIButton *)button {
    button.selected = !button.selected;
}

- (void)settingClick {
    //跳转到设置界面
    CCSettingsViewController *settingsVc = [[CCSettingsViewController alloc] init];
    [self.navigationController pushViewController:settingsVc animated:YES];
}




@end
